﻿using BLL;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.BLL;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.自定义类;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 纺织贸易管理系统.新增窗体
{
    public partial class 流转卡拆分 : Sunny.UI.UIForm 
    {
        public FormUseful Useful { get; set; }
        public Liuzhuancard liuzhuancard { get; set; }
        private List<StockTable> stocks = new List<StockTable>();
        List<liuzhuanmingxi> liuzhuangkamingxi = new List<liuzhuanmingxi>();
        public List<danjumingxitable> danjumingxis { get; set; } = new List<danjumingxitable>();
        public DanjuTable danju { get; set; } = new DanjuTable();
        private List<int> SelectIndex = new List<int>();
        private List<danjumingxitable> newmingxis = new List<danjumingxitable>();
        public 流转卡拆分()
        {
            InitializeComponent();
            uiDatePicker1.Value = DateTime.Now;
            cmbjitai.Items.AddRange(MachineTableService.GetMachineTablelst("select * from machinetable").Select(x => x.MachineID).ToArray());
            cmblianxiren.Items.AddRange(YuanGongTableService.GetYuanGongTablelst().Select(x => x.Xingming).ToArray());
            try
            {
                CreateGrid.CreateDatagridview("流转卡", gridView2);
                gridView2.Columns.Add(cxz);                
                CreateGrid.CreateDatagridview("流转卡", uiDataGridView1 , "gridView2");
                uiDataGridView1.Columns.Add(cxz1);
            }
            catch
            {
                配置列ToolStripMenuItem_Click(null, null);
            }
            gridView2.DataSource = danjumingxis;
            gridControl1.DataSource = liuzhuangkamingxi;
        }
        private void 流转卡_Load(object sender, EventArgs e)
        {
            InitForm();
        }
        private void InitForm()
        {
            SelectIndex.Clear();
            //if (Useful == FormUseful.复制)
            //{
            //    txtdanhao.Text = BianhaoBLL.CreateLiuzhuanKahao(FirstLetter.流转卡, uiDatePicker1.Value);
            //    txtkehu.Text = danju.ksmc;
            //    txtkezhong.Text = danjumingxis.Count > 0 ? danjumingxis[0].kezhong : string.Empty;
            //    txtmenfu.Text = danjumingxis.Count > 0 ? danjumingxis[0].menfu : string.Empty;
            //    txtpingming.Text = danjumingxis.Count > 0 ? danjumingxis[0].pingming : string.Empty;
            //    txtckmc.Text = danju.ckmc;
            //    txtbeizhu.Text = danju.bz;
            //    cmbJincangyuanying.Text = danju.jiagongleixing;
            //    txtshuliang.Text = danjumingxis.Sum(x => x.chengpingmishu).ToString();
            //    txtyanse.Text = danjumingxis.Count > 0 ? danjumingxis[0].yanse : string.Empty; ;
            //    var gongyis = TechnologyTableService.GetTechnologyTablelst("select * from Technologytable");
            //    foreach (var g in gongyis)
            //    {
            //        liuzhuangkamingxi.Add(new liuzhuanmingxi() { CardNum = txtdanhao.Text, GongyiYaoqiu = g.Technology, Cishu = 1 });
            //    }
            //    //danjumingxis.AddRange(new danjumingxitable[30] );            
            //    var count = danjumingxis.Count;
            //    foreach (var d in danjumingxis )
            //    {
            //        d.danhao = txtdanhao.Text;
            //    }
            //    for (int i = 0; i < 30 - count; i++)
            //    {
            //        danjumingxis.Add(new danjumingxitable { danhao = txtdanhao.Text });
            //    }
            //    gridControl1.RefreshDataSource();
            //    gridView2.DataSource = danjumingxis;
            //}
            //else
            //{
                if (Useful == FormUseful.修改)
                {
                    txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.流转卡, uiDatePicker1.Value,DanjuLeiXing.流转卡 ); 
                    txtbeizhu.Text = liuzhuancard.Beizhu;
                    txtkehu.Text = liuzhuancard.Customer;
                    txtkezhong.Text = liuzhuancard.Kezhong;
                    txtmenfu.Text = liuzhuancard.Menfu;
                    txtpingming.Text = liuzhuancard.Pingming;
                    txtshuliang.Text = liuzhuancard.Shuliang.ToString();
                    txtyanse.Text = liuzhuancard.Color;
                    txtckmc.Text = liuzhuancard.StockName;
                    cmbJincangyuanying.Text = liuzhuancard.JiaGongYuanYing;
                    var gongyis = TechnologyTableService.GetTechnologyTablelst("select * from Technologytable");
                    foreach (var g in gongyis)
                    {
                        liuzhuangkamingxi.Add(new liuzhuanmingxi() { CardNum = txtdanhao.Text, GongyiYaoqiu = g.Technology, Cishu = 1, Beizhu = "" });
                    }
                    var mingxis = liuzhuanmingxiService.Getliuzhuanmingxilst(x => x.CardNum ==liuzhuancard.CardNum );
                    foreach (var m in mingxis)
                    {
                        var z = liuzhuangkamingxi.Where(x => x.GongyiYaoqiu == m.GongyiYaoqiu).ToList().Count >0? liuzhuangkamingxi.Where(x => x.GongyiYaoqiu == m.GongyiYaoqiu).ToList()[0]:new liuzhuanmingxi ();
                        z.Employee = m.Employee;
                        z.Cishu = m.Cishu;
                        z.Beizhu = m.Beizhu;
                        z.MachineID = m.MachineID;
                    }
                    gridControl1.DataSource = liuzhuangkamingxi;
                    gridControl1.RefreshDataSource();
                    danjumingxis = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao ==liuzhuancard.CardNum );               
                    gridView2.DataSource = danjumingxis;

                }
            //    else
            //    {
            //        if (Useful == FormUseful.新增)
            //        {
            //            txtdanhao.Text = BianhaoBLL.CreateLiuzhuanKahao(FirstLetter.流转卡, uiDatePicker1.Value);
            //            txtbeizhu.Text = "";
            //            txtkehu.Text = "";
            //            txtkezhong.Text = string.Empty;
            //            txtmenfu.Text = string.Empty;
            //            txtpingming.Text = string.Empty;
            //            txtckmc.Text = string.Empty;
            //            txtshuliang.Text = danjumingxis.Sum(x => x.chengpingmishu).ToString();
            //            txtyanse.Text = danjumingxis.Count > 0 ? danjumingxis[0].yanse : string.Empty; ;
            //            var gongyis = TechnologyTableService.GetTechnologyTablelst("select * from Technologytable");

            //            foreach (var g in gongyis)
            //            {
            //                liuzhuangkamingxi.Add(new liuzhuanmingxi() { CardNum = txtdanhao.Text, GongyiYaoqiu = g.Technology, Cishu = 1 });
            //            }
            //            //danjumingxis.AddRange(new danjumingxitable[30] );            
            //            danjumingxis.Clear();
            //            var count = danjumingxis.Count;
            //            for (int i = 0; i < 30 - count; i++)
            //            {
            //                danjumingxis.Add(new danjumingxitable { danhao = txtdanhao.Text });
            //            }
            //            gridControl1.RefreshDataSource();
            //            gridView2.DataSource = danjumingxis;
            //        }
            //        else
            //        {
            //            if (Useful == FormUseful.查看)
            //            {
            //                txtdanhao.Text = BianhaoBLL.CreateLiuzhuanKahao(FirstLetter.流转卡, uiDatePicker1.Value);
            //                //danjumingxis.AddRange(new danjumingxitable[30] );            
            //                var count = danjumingxis.Count;
            //                foreach (var d in danjumingxis)
            //                {
            //                    d.danhao = txtdanhao.Text;
            //                }
            //                for (int i = 0; i < 30 - count; i++)
            //                {
            //                    danjumingxis.Add(new danjumingxitable { danhao = txtdanhao.Text });
            //                }
            //                gridControl1.RefreshDataSource();
            //                gridView2.DataSource = danjumingxis;
            //            }
            //        }
            //    }
            //}
        }

        private void 选择布料ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 工序选择();
            fm.ShowDialog();
            foreach (var t in fm.technologyTables)
            {
                txtbeizhu.Text += t.Technology + "+";
            }
            txtbeizhu.Text = txtbeizhu.Text.Substring(0, txtbeizhu.Text.Length - 1);
        }

        private void 打印编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (后整理管理系统.自定义类.PrintService.CheckPrintDesignAccess())
            {
                Printer(PrintModel.Design);
            }
        }
        private void Printer(PrintModel printModel)
        {
            gridView1.CloseEditor();
            gridView2.EndEdit();
            //if(gridView2.GetSelectedRows ().Length  ==0)
            //{
            //    Sunny.UI.UIMessageBox.ShowError("没有任何布料别选中！\r\n请把要开流转卡的布料选中,布料前面打勾");
            //    return;
            //}
            var fs = new FastReport.Report();
            DataTable liuzhuandan = new DataTable("流转单信息");
            liuzhuandan.Columns.Add("流转卡号");
            liuzhuandan.Columns.Add("客户名称");
            liuzhuandan.Columns.Add("日期", typeof(DateTime));
            liuzhuandan.Columns.Add("品名");
            liuzhuandan.Columns.Add("克重");
            liuzhuandan.Columns.Add("门幅");
            liuzhuandan.Columns.Add("颜色");
            liuzhuandan.Columns.Add("数量", typeof(decimal));
            liuzhuandan.Columns.Add("备注");
            liuzhuandan.Rows.Add();
            //赋值
            liuzhuandan.Rows[0]["流转卡号"] = txtdanhao.Text;
            liuzhuandan.Rows[0]["客户名称"] = txtkehu.Text;
            liuzhuandan.Rows[0]["日期"] = uiDatePicker1.Value;
            liuzhuandan.Rows[0]["品名"] = txtpingming.Text;
            liuzhuandan.Rows[0]["克重"] = txtkezhong.Text;
            liuzhuandan.Rows[0]["门幅"] = txtmenfu.Text;
            liuzhuandan.Rows[0]["颜色"] = txtyanse.Text;
            liuzhuandan.Rows[0]["数量"] = Convert.ToDecimal(txtshuliang.Text);
            liuzhuandan.Rows[0]["备注"] =cmbJincangyuanying.Text =="首次加工"? txtbeizhu.Text:cmbJincangyuanying.Text +" "+txtbeizhu.Text ;
            //
            DataSet ds = new DataSet();
            ds.Tables.Add(liuzhuandan);
            ///明细
            DataTable mingxi = new DataTable("流转卡明细");
            try
            {
                for (var col = 0; col < gridView1.Columns.Count; col++)
                {
                    mingxi.Columns.Add(gridView1.Columns[col].Caption);
                }
                //明细赋值
                for (var row = 0; row < gridView1.RowCount; row++)
                {
                    var index = mingxi.Rows.Add();
                    for (var col = 0; col < gridView1.Columns.Count; col++)
                    {
                        index[col] = gridView1.GetRowCellValue(row, gridView1.Columns[col].FieldName);
                    }
                }

                ds.Tables.Add(mingxi);
                //布料信息
                /// 明细
                DataTable bulioaos = new DataTable("布料明细");
                for (var col = 0; col < gridView2.Columns.Count; col++)
                {
                    bulioaos.Columns.Add(gridView2.Columns[col].HeaderText );
                }
                //布料明细赋值
                  foreach (DataGridViewRow row in uiDataGridView1.Rows)
                {                   
                        var index = bulioaos.Rows.Add();
                        for (var col = 0; col < gridView2.Columns.Count; col++)
                        {
                            index[col] = row.Cells[col].Value;
                        }
                }
                ds.Tables.Add(bulioaos);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowErrorBox (  ex.Message);
            }
            fs.RegisterData(ds);
            if (File.Exists(Application.StartupPath + "\\Report\\流转单.frx"))
            {
                fs.Load(Application.StartupPath + "\\Report\\流转单.frx");
            }
            else
            {
                fs.Design();
                string path = Application.StartupPath + "\\Report\\流转单.frx";
                var arr = path.Split('\\');
                ReportTableService.DeleteReportTable(x => x.reportName == arr[arr.Length - 1] && x.reportStyle == ReportService.报表);
                ReportService.LoadReport(path, new ReportTable { reportStyle = ReportService.报表, reportName = arr[arr.Length - 1] });
                return;
            }
            try
            {
                switch (printModel)
                {
                    case PrintModel.Design:
                        fs.Design();
                        break;
                    case PrintModel.Privew:
                        fs.Show();
                        break;
                    case PrintModel.Print:
                        fs.Print();
                        break;
                }
            }
            catch 
            {
                fs.Design();
            }
            if (Sunny.UI.UIMessageBox.ShowAsk("是否直接保存该单据？\r\n保存按确定，否则按取消"))
            {
                新增ToolStripMenuItem_Click(null, null);
            }
        }

        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Printer(PrintModel.Privew );
        }

        private void 直接打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Printer(PrintModel.Print );
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.CloseEditor();
            gridView2.EndEdit();

          if(newmingxis.Count ==0)
            {
                Sunny.UI.UIMessageBox.ShowError("没有任何布料被分出来,\r\n请把要分的布料放到下面去");
                return;
            }
            if (string.IsNullOrWhiteSpace (txtbeizhu.Text ) )
            {
                Sunny.UI.UIMessageBox.ShowError("请填写生产工艺要求");
                return;
            }
            if(string.IsNullOrWhiteSpace ( cmbJincangyuanying.Text ) )
            {
                Sunny.UI.UIMessageBox.ShowError("请选择进仓原因！");
                return;
            }
            if(Useful!=FormUseful.修改 && cmbJincangyuanying.Text !="首次加工" )
            {
                if (LiuzhuancardService.GetLiuzhuancardlst(x => x.CardNum == txtdanhao.Text).Count > 0)
                {
                    Sunny.UI.UIMessageBox.ShowInfo($"该流转卡号{txtdanhao.Text }已经存在！\r\n系统将重新生成单号");
                    var cardNum =  BianhaoBLL.CreatDanhao(FirstLetter.流转卡, uiDatePicker1.Value,DanjuLeiXing.流转卡 );
                    while (LiuzhuancardService.GetLiuzhuancardlst ( x=>x.CardNum==cardNum ).Count >0)
                    {
                        cardNum = BianhaoBLL.CreatDanhao(FirstLetter.流转卡, uiDatePicker1.Value,DanjuLeiXing.流转卡 );
                    }
                    txtdanhao.Text = cardNum;
                    foreach (var d in danjumingxis)
                    {
                        d.danhao = txtdanhao.Text;
                    }
                }
            }
            var liuzhuandan = new Liuzhuancard()
            {
                Beizhu = txtbeizhu.Text,
                CardNum = txtdanhao.Text,
                Card_Date = uiDatePicker1.Value,
                Color = txtyanse.Text,
                Customer = txtkehu.Text,
                Ganghao = string.Empty,
                Kezhong = txtkezhong.Text,
                Menfu = txtmenfu.Text,
                Pingming = txtpingming.Text,
                StockName = txtckmc.Text,
                JiaGongYuanYing =cmbJincangyuanying.Text ,
                Shuliang = Convert.ToDecimal(txtshuliang.Text),
                ID=liuzhuancard !=null? liuzhuancard.ID :0,
                own = Tools.CurrentUser.user.YHBH
        };
            var mingxi = new List<liuzhuanmingxi>();
            //MessageBox.Show(gridView1.GetRowCellValue(0, colGongyiYaoqiu.FieldName).ToString());
            for (int row = 0; row < gridView1.RowCount; row++)
            {
                mingxi.Add(new liuzhuanmingxi()
                {
                    Beizhu = gridView1.GetRowCellValue(row, colBeizhu.FieldName) == null ? string.Empty : gridView1.GetRowCellValue(row, colBeizhu.FieldName).ToString(),
                    CardNum = txtdanhao.Text,
                    Employee = gridView1.GetRowCellValue(row, colEmployee.FieldName) == null ? string.Empty : gridView1.GetRowCellValue(row, colEmployee.FieldName).ToString(),
                    GongyiYaoqiu = gridView1.GetRowCellValue(row, colGongyiYaoqiu.FieldName) == null ? string.Empty : gridView1.GetRowCellValue(row, colGongyiYaoqiu.FieldName).ToString(),
                    MachineID = gridView1.GetRowCellValue(row, colMachineID.FieldName) == null ? string.Empty : gridView1.GetRowCellValue(row, colMachineID.FieldName).ToString(),
                    Cishu = Convert.ToInt32(gridView1.GetRowCellValue(row, colCishu.FieldName)),
                });
            }
            List<danjumingxitable> resmingxis = new List<danjumingxitable>();          
            流转单BLL.拆分保存(liuzhuandan, mingxi , stocks, newmingxis);
            string oldCardNum = danjumingxis[0].danhao;
            LiuzhuancardService.UpdateLiuzhuancard($"Shuliang ='{danjumingxis.Sum (y=>y.chengpingmishu )}'", x => x.CardNum ==oldCardNum );
            this.Close();
            this.Dispose();
        }                                                                                                                                                                                          
        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView2) { formname = this.Name, Obj = new danjumingxitable() };
            fm.ShowDialog();
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView2);
        }

        private void ButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (gridView2.CurrentRow.Index  >= 0)
            {
                if (txtckmc.Text == "")
                {
                    MessageBox.Show("请先选择仓库名称", "错误", MessageBoxButtons.OK);
                    return;
                }
                var fm = new 库存选择() { StockName = txtckmc.Text };
                fm.ShowDialog();
                var i = gridView2.CurrentRow.Index ;
                
                foreach (var pingzhong in fm.pingzhong)
                {
                   
                    danjumingxis[i].bizhong = "人民币￥";
                    danjumingxis[i].Bianhao = pingzhong.BH;
                    danjumingxis[i].guige = pingzhong.GG;
                    danjumingxis[i].chengfeng = pingzhong.CF;
                    danjumingxis[i].pingming = pingzhong.PM;
                    danjumingxis[i].kezhong = pingzhong.KZ;
                    danjumingxis[i].menfu = pingzhong.MF;
                    danjumingxis[i].danwei = "米";
                    danjumingxis[i].ContractNum = pingzhong.ContractNum;
                    danjumingxis[i].CustomName = pingzhong.CustomName;
                    danjumingxis[i].OrderNum = pingzhong.orderNum;
                    danjumingxis[i].kuanhao = pingzhong.kuanhao;
                    danjumingxis[i].houzhengli = pingzhong.houzhengli;
                    danjumingxis[i].yanse = pingzhong.YS;
                    danjumingxis[i].Chengpingyanse = pingzhong.YS;
                    danjumingxis[i].ganghao = pingzhong.GH;
                    danjumingxis[i].chengpingjuanshu = pingzhong.JS;
                    danjumingxis[i].chengpingmishu = pingzhong.MS;
                    danjumingxis[i].Kuwei = pingzhong.Kuwei;
                    danjumingxis[i].Huahao = pingzhong.Huahao;
                    danjumingxis[i].ColorNum = pingzhong.ColorNum;
                    danjumingxis[i].CustomerColorNum = pingzhong.CustomerColorNum;
                    danjumingxis[i].CustomerPingMing = pingzhong.CustomerPingMing;
                    danjumingxis[i].rq = uiDatePicker1.Value;
                    danjumingxis[i].offerid = pingzhong.RKDH;
                    danjumingxis[i].rkdh = pingzhong.RKDH;
                    danjumingxis[i].IsHanshui = IsHanshuiModel.含税;
                    danjumingxis[i].ID = pingzhong.ID;
                    danjumingxis[i].hanshuidanjia = OrderDetailTableService.GetOneOrderDetailTable(x => x.OrderNum == pingzhong.orderNum && x.sampleNum == pingzhong.BH && x.Kuanhao == pingzhong.kuanhao
                    && x.ColorNum == pingzhong.ColorNum && x.color == pingzhong.YS && x.Huahao == pingzhong.Huahao).price;
                    danjumingxis[i].hanshuiheji = danjumingxis[i].hanshuidanjia * pingzhong.MS;
                    i++;
                    if (i == danjumingxis.Count - 1)
                        for (int j = 0; j < 30; j++)
                        {
                            danjumingxis.Add(new danjumingxitable() { danhao = txtdanhao.Text, rq = Convert.ToDateTime(uiDatePicker1.Text) });
                        }
                }
                var mingxi = danjumingxis[0];
                txtkezhong.Text = mingxi.kezhong ;
                txtpingming.Text = mingxi.pingming  ;
                txtyanse.Text = mingxi.yanse ;
                txtmenfu.Text = mingxi.menfu ;
                txtguige.Text = mingxi .guige ;
                txtshuliang.Text = danjumingxis.Sum(x => x.chengpingmishu).ToString();
                gridView2.EndEdit ();
                自动加载工艺();
            }
        }

        private void txtckmc_ButtonClick(object sender, EventArgs e)
        {
            var fm = new 仓库选择();
            fm.ShowDialog();
            txtckmc.Text = fm.stock.mingcheng;
        }

        private void 选择布料ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ButtonEdit1_ButtonClick(null, null);
        }

        private void txtkehu_ButtonClick(object sender, EventArgs e)
        {
            var fm = new 客户选择() { linkman = new LXR() { ZJC = "", MC = "" } };
            fm.ShowDialog();
            txtkehu.Text = fm.linkman.MC;
        }

        private void gridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            txtshuliang.Text = danjumingxis.Sum(x => x.chengpingmishu).ToString();
        }

        private void 复制单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.Focus();
            SendKeys.Send("^c");
        }

        private void 粘贴单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.Focus();
            SendKeys.Send("^v");
        }

   
        private void 自动加载工艺()
        {
            var liuzhuancard = LiuzhuancardService.GetOneLiuzhuancard(x=>x.Customer==txtkehu.Text &&x.Pingming==txtpingming.Text &&x.Color ==txtyanse.Text );
            if(string.IsNullOrWhiteSpace (liuzhuancard.Beizhu ))
            {
                liuzhuancard = LiuzhuancardService.GetOneLiuzhuancard(x => x.Customer == txtkehu.Text && x.Pingming == txtpingming.Text );
                txtbeizhu.Text = liuzhuancard.Beizhu;
            }
            else
            {
                txtbeizhu.Text = liuzhuancard.Beizhu;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            uiContextMenuStrip1.Items.Clear();
            var liuzhuancard = LiuzhuancardService.GetLiuzhuancardlst(x => x.Customer == txtkehu.Text && x.Pingming == txtpingming.Text );
            foreach (var liu in liuzhuancard.Select (x=>x.Beizhu ).Distinct ())
            {
                ToolStripMenuItem item = new ToolStripMenuItem(liu);
                item.Click += Item_Click;
                uiContextMenuStrip1.Items.Add(item);
            }
            uiButton1.ShowContextMenuStrip(uiContextMenuStrip1, 0, uiButton1.Height);
        }
        private void Item_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            txtbeizhu.Text = item.Text;
        }

        private void txtkehu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var fm = new 客户选择() { linkman = new LXR() { ZJC = txtkehu.Text, MC = "" } };
                fm.ShowDialog();
                txtkehu.Text = fm.linkman.MC;
            }
        }
        private void txtCkmc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var fm = new 客户选择() { linkman = new LXR() { ZJC = txtkehu.Text, MC = "" } };
                fm.ShowDialog();
                txtkehu.Text = fm.linkman.MC;
            }
        }

        private void gridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            try
            {
                gridView2.EndEdit();
              
                //List<int> selectList = new List<int>();
                foreach(DataGridViewRow row in gridView2.Rows ) 
                { 
                    if (row.Cells[cxz.Name].Value!=null&&(Boolean)row.Cells [cxz.Name].Value)
                    {
                        newmingxis.Add(danjumingxis[row.Index]);
                    }
                }
                uiDataGridView1.DataSource = null;
                uiDataGridView1.DataSource = newmingxis;
                int rowCount = danjumingxis.Count;
                int k = 0;//标记减少的行数
                for (int i = 0; i < rowCount; i++)
                {
                    object obj = this.gridView2 .Rows [i - k].Cells[cxz.Name ].Value;//获取选中行的值i-k保持行下标不变
                    if (obj != null &&(Boolean) obj)//True必须这样写 不能写成true
                    {
                        int rowIndex = i - k;
                        danjumingxis.RemoveAt(rowIndex);
                        gridView2.DataSource = null;
                        gridView2.DataSource =danjumingxis ;
                        k++;//减少的行数加1
                    }
                }
                txtshuliang.Text = newmingxis.Sum(x => x.chengpingmishu).ToString();
            }
            catch(Exception ex)
            {
                Sunny.UI.UIMessageBox.Show(ex.Message);
            }
        }

        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            try
            {
                uiDataGridView1 .EndEdit();
                //List<int> selectList = new List<int>();
                foreach (DataGridViewRow row in uiDataGridView1 .Rows)
                {
                    if (row.Cells[cxz1.Name].Value != null && (Boolean)row.Cells[cxz1.Name].Value)
                    {
                        danjumingxis .Add(newmingxis [row.Index]);
                    }
                }
                gridView2.DataSource = null;
                gridView2 .DataSource = danjumingxis ;
                int rowCount = newmingxis .Count;
                int k = 0;//标记减少的行数
                for (int i = 0; i < rowCount; i++)
                {
                    object obj = this.uiDataGridView1 .Rows[i - k].Cells[cxz1.Name].Value;//获取选中行的值i-k保持行下标不变
                    if (obj != null && (Boolean)obj)//True必须这样写 不能写成true
                    {
                        int rowIndex = i - k;
                        newmingxis .RemoveAt(rowIndex);
                        k++;//减少的行数加1
                        uiDataGridView1.DataSource = null;
                        uiDataGridView1.DataSource = newmingxis;
                    }
                }              
                txtshuliang.Text = newmingxis.Sum(x => x.chengpingmishu).ToString();
            }
            catch (Exception ex)
            {
                Sunny.UI.UIMessageBox.Show(ex.Message);
            }
        }
    }
}
